package com.wuwei.elearning.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuwei.elearning.exception.ElearningMysqlException;
import com.wuwei.elearning.question.domain.QuestionItem;
import com.wuwei.elearning.question.domain.TestResult;
import com.wuwei.elearning.question.mapper.QuestionItemMapper;
import com.wuwei.elearning.question.mapper.TestResultMapper;
import com.wuwei.elearning.question.vo.GetResultVo;
import com.wuwei.elearning.question.vo.PageVo;
import com.wuwei.elearning.setting.mapper.UserMapper;
import com.wuwei.elearning.utils.result.Result;
import com.wuwei.elearning.utils.token.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 考试结果存储的表 前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-01-18
 */
@RestController
@RequestMapping("/elearning/testResult")
@CrossOrigin
public class TestResultController {

    @Resource
    private TestResultMapper testResultMapper;


    @Autowired
    private HttpServletRequest request;

    /**
     * 分页获取test_result列表的方法
     * @param resultVoPageVo
     * @return
     */
    @PostMapping("getList")
    public Result< List<TestResult>> getList(@RequestBody PageVo<GetResultVo> resultVoPageVo ){
        //获取用户名
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        int currentPage = resultVoPageVo.getCurrentPage();
        int pageSize = resultVoPageVo.getPageSize();
        IPage<TestResult> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        GetResultVo getResultVo = resultVoPageVo.getParams();
        LambdaQueryWrapper<TestResult> queryWrapper = bulidQuery(getResultVo, account);
        //todo 这里的buildQuery没有奏效
        IPage<TestResult> testResultIPage = testResultMapper.selectPage(page, queryWrapper);
        return Result.ok(testResultIPage.getRecords());
    }

    /**
     * 获取所有考试成绩的方法
     * 该方法学员和教员都可以调用
     * 会在参数中判断role属性，如果role属性是teacher，则可以查看所有考试成绩
     * 如果role属性是student，则只能查看自己的考试成绩
     * @param getResultVo
     * @return
     */
    @PostMapping("getTotal")
    public Result<Long> getTotal (@RequestBody GetResultVo getResultVo){

        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        LambdaQueryWrapper<TestResult> queryWrapper = bulidQuery(getResultVo, account);
        Long count = testResultMapper.selectCount(queryWrapper);
        return Result.ok(count);
    }

    /*
    如果role属性是teacher，则可以查看所有考试成绩
     * 如果role属性是student，则只能查看自己的考试成绩
     */
    private LambdaQueryWrapper<TestResult> bulidQuery(@RequestBody GetResultVo getResultVo, Long account) {
        LambdaQueryWrapper<TestResult> queryWrapper = new LambdaQueryWrapper<>();
        String role = getResultVo.getRole();
        //获取用户名
        if ("student".equals(role)) {
            queryWrapper.eq(TestResult::getAccount,account);
        }
        queryWrapper
                .le(getResultVo.getEndTime()!=null,TestResult::getCreateTime,getResultVo.getEndTime())
                .ge(getResultVo.getStartTime()!=null,TestResult::getCreateTime,getResultVo.getStartTime())
                .like(getResultVo.getTitle() != null,TestResult::getTitle,getResultVo.getTitle())
                .orderByDesc(TestResult::getCreateTime);
        return queryWrapper;
    }

}

